import { Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, CoolController } from '@cool-midway/core';
import { UserFootprintEntity } from '../../entity/footprint';
import { UserFootprintService } from '../../service/footprint';
import { Body } from '@midwayjs/core';
import { GoodsInfoEntity } from '../../../goods/entity/info';
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserFootprintEntity,
  service: UserFootprintService,
  insertParam: ctx => {
    return {
      userId: ctx.user.id,
    };
  },
  listQueryOp: {
    where: async ctx => {
      return [['userId =:userId', { userId: ctx.user.id }]];
    },
    addOrderBy: {
      isDefault: 'DESC',
    },
  },
  pageQueryOp: {
    select: ['a.userId', 'a.visitCount', 'a.lastVisitTime', 'goods.*'],
    where: async ctx => {
      return [['userId =:userId', { userId: ctx.user.id }]];
    },
    join: [
      {
        entity: GoodsInfoEntity,
        alias: 'goods',
        condition: 'goods.id = a.goodsId',
      },
    ],
  },
})
export class AppUserFootprintController extends BaseController {
  @Inject()
  userFootprintService: UserFootprintService;

  @Inject()
  ctx;

  @Post('/recordFootprint')
  async recordFootprint(@Body('goodsId') goodsId: number) {
    return this.ok(await this.userFootprintService.recordFootprint(this.ctx.user.id, goodsId));
  }
}
